import { ref } from 'vue';

import { useAuthStore } from '@/store/modules/auth';

import { get } from '@/service/api/api';

let topFormModel;
let bottomFormModel;
const lotType = ref('searchInput');
const lotids = ref([]);
const moduleNoSearchData = ref(null);
const authStore = useAuthStore();

export default {
  url: '/qms/immersionGold/potion/list',
  addUrl: '/qms/immersionGold/potion/add',
  editUrl: '/qms/immersionGold/potion/update',
  delUrl: '/qms/immersionGold/potion/deleteBatch',
  editMethod: 'put',
  searchFormList: [
    {
      type: 'input',
      key: 'moduleNo',
      label: '生产型号',
      placeholder: '请输入生产型号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'input',
      key: 'lotId',
      label: 'LOT号',
      placeholder: '请输入LOT号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'input',
      key: 'serialNum',
      label: '单据号',
      placeholder: '请输入单据号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'select',
      key: 'state',
      label: '审核状态',
      placeholder: '请选择审核状态',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4,
      selectOptions: [
        {
          value: '0',
          label: '待领班审核'
        },
        {
          value: '1',
          label: '待修改'
        },
        {
          value: '2',
          label: '待主管审核'
        },
        {
          value: '3',
          label: '审核完成'
        }
      ]
    },

    {
      type: 'dateRangePicker',
      key: 'time',
      label: '日期',
      span: 24,
      md: 12,
      lg: 10,
      xl: 8,
      xxl: 5
    }
  ],
  searchParam: ref({
    moduleNo: null,
    lotId: null,
    state: null,
    time: null
  }),
  column: [
    {
      key: 'dataIndex',
      dataIndex: 'dataIndex',
      title: '序号',
      width: 70,
      fixed: 'left',
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'moduleNo',
      dataIndex: 'moduleNo',
      title: '生产型号',
      width: 150,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'lotId',
      dataIndex: 'lotId',
      title: 'LOT号',
      width: 150,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'serialNum',
      dataIndex: 'serialNum',
      title: '单据号',
      width: 130,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'state',
      dataIndex: 'state',
      title: '状态',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return ['待领班审核', '待修改', '待主管审核', '审核完成'][record.state];
      }
    },
    {
      key: 'modifyTime',
      dataIndex: 'modifyTime',
      title: '录入时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'meridional',
      dataIndex: 'meridional',
      title: '尺寸长(MM)',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'zonal',
      dataIndex: 'zonal',
      title: '尺寸宽(MM)',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'quantity',
      dataIndex: 'quantity',
      title: '数量',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'glodRequired',
      dataIndex: 'glodRequired',
      title: '金厚要求(u")',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'nickelRequired',
      dataIndex: 'nickelRequired',
      title: '镍厚要求(u")',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'immgoldArea',
      dataIndex: 'immgoldArea',
      title: '当缸面积(m2)',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'addArea',
      dataIndex: 'addArea',
      title: '累计面积(m2)',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'immgoldPlatingAreaRate',
      dataIndex: 'immgoldPlatingAreaRate',
      title: '受镀面积比(%)',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'immgoldPlatingArea',
      dataIndex: 'immgoldPlatingArea',
      title: '受镀面积(m2)',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'overload',
      dataIndex: 'overload',
      title: '负载(0.2-0.6dm2/L)',
      width: 150,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'immgoldLines',
      dataIndex: 'immgoldLines',
      title: '线别',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'shiftType',
      dataIndex: 'shiftType',
      title: '班别',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return ['白班', '夜班'][record.shiftType];
      }
    },

    {
      key: 'degreasing',
      dataIndex: 'degreasing',
      title: '除油',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'microetch',
      dataIndex: 'microetch',
      title: '微蚀',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'activate',
      dataIndex: 'activate',
      title: '活化',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'goldcylinder',
      dataIndex: 'goldcylinder',
      title: '后浸',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'pickling',
      dataIndex: 'pickling',
      title: '酸洗',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'preSoak',
      dataIndex: 'preSoak',
      title: '预浸',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'operator',
      dataIndex: 'operator',
      title: '操作员',
      width: 110,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'foreman',
      dataIndex: 'foreman',
      title: '领班审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'foremanTime',
      dataIndex: 'foremanTime',
      title: '领班审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examiner',
      dataIndex: 'examiner',
      title: '主管审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examinerTime',
      dataIndex: 'examinerTime',
      title: '主管审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'remark',
      dataIndex: 'remark',
      title: '备注',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'createTime',
      dataIndex: 'createTime',
      title: '创建时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateTime',
      dataIndex: 'updateTime',
      title: '修改时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateBy',
      dataIndex: 'updateBy',
      title: '修改人',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    }
  ],

  scroll: { x: 2500, y: 'auto' },
  topFormList: [
    // 用于获取model
    {
      type: 'getModel',
      hidden: true,
      handle(topModel, personModle) {
        topFormModel = topModel;
        bottomFormModel = personModle;
      }
    },
    {
      type: lotType,
      key: 'lotId',
      label: 'Lot号',
      span: 8,
      rule: {
        required: true,
        message: '请输入Lot号',
        trigger: 'change'
      },
      searchKey: 'lotNumber',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      disabled: false,
      transfromSearch(model, searchData) {
        topFormModel = model;
        moduleNoSearchData.value = searchData;
        Object.keys(model.value).forEach(item => {
          model.value[item] = searchData[item] || model.value[item];
        });
      },
      selectOptions: lotids,
      showSelectOtherBtn: true,
      selectOtherBtnText: '查询',
      search: true,
      handleSelectOtherBtnClick: () => {
        get('/rcerp/agv/erp/findByLotNo', {
          lotNumber: topFormModel.value.lotId
        }).then(({ data, error }) => {
          if (!error) {
            Object.keys(topFormModel.value).forEach(item => {
              topFormModel.value[item] = data[item] || topFormModel.value[item];
            });
          }
        });
      }
    },

    {
      type: 'searchInput',
      key: 'moduleNo',
      label: '生产型号',
      span: 8,
      rule: {
        required: true,
        message: '请输入生产型号',
        trigger: 'change'
      },
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      transfromSearch(model, searchData) {
        topFormModel = model;
        moduleNoSearchData.value = searchData;
        lotids.value = searchData.map(item => {
          return {
            label: item.lotNo,
            value: item.lotNo
          };
        });
        lotType.value = 'select';
      }
    },

    {
      type: 'datePicker',
      key: 'modifyTime',
      label: '录入时间',
      span: 8,
      rule: {
        required: true,
        message: '请选择录入时间',
        trigger: 'change'
      },
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showTime: true
    },

    {
      type: 'input',
      key: 'meridional',
      label: '尺寸长(MM)',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'zonal',
      label: '尺寸宽(MM)',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'input',
      key: 'quantity',
      label: '数量',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      change() {
        topFormModel.value.immgoldArea = (
          (topFormModel.value.meridional * topFormModel.value.zonal * topFormModel.value.quantity) /
          1000000
        ).toFixed(3);

        topFormModel.value.immgoldPlatingArea = (
          (topFormModel.value.immgoldPlatingAreaRate / 100) *
          topFormModel.value.immgoldArea
        ).toFixed(3);
        // overload 
        topFormModel.value.overload = ((topFormModel.value.immgoldPlatingArea / 3000) * 100).toFixed(3);
        console.log(111, topFormModel.value.overload);
      }
    },
    {
      type: 'input',
      key: 'glodRequired',
      label: '金厚要求(u")',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'nickelRequired',
      label: '镍厚要求(u")',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'input',
      key: 'immgoldArea',
      label: '当缸面积(m2)',
      span: 8,
      disabled: true
    },

    {
      type: 'input',
      key: 'addArea',
      label: '累计面积(m2)',
      span: 8,
      disabled: true
    },
    {
      type: 'input',
      key: 'immgoldPlatingAreaRate',
      label: '受镀面积比(%)',
      span: 8,
      disabled: true
    },

    {
      type: 'input',
      key: 'immgoldPlatingArea',
      label: '受镀面积(m2)',
      span: 8,
      disabled: true
    },
    {
      type: 'input',
      key: 'overload',
      label: '负载(0.2-0.6dm2/L)',
      span: 8,
      disabled: true
    },

    {
      type: 'input',
      key: 'degreasing',
      label: '除油',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'microetch',
      label: '微蚀',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'input',
      key: 'activate',
      label: '活化',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'input',
      key: 'goldcylinder',
      label: '后浸',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'immgoldLines',
      label: '线别',
      span: 8,
      // rule: {
      //   required: true,
      //   message: '请输入线别',
      //   trigger: 'change'
      // },
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'pickling',
      label: '酸洗',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'input',
      key: 'preSoak',
      label: '预浸',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'radio',
      key: 'shiftType',
      label: '班别',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: '白班',
          value: '0'
        },
        {
          label: '夜班',
          value: '1'
        }
      ]
    },
    {
      type: 'textarea',
      key: 'remark',
      label: '备注',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      textareaRows: 1,
      span: 24
    }
  ],
  bottomList: [
    {
      type: 'input',
      key: 'operator',
      label: '操作员',
      defaultValue: authStore.userInfo.user.nickName || authStore.userInfo.user.userName,
      disabled: true,
      span: 8
    },
    {
      type: 'input',
      key: 'foreman',
      label: '领班审核',
      disabled: true,
      span: 8
    },
    {
      type: 'input',
      key: 'examiner',
      label: '主管审核',
      disabled: true,
      span: 8
    }
  ],
  modalHeight: '80vh',
  modleSearchUrlObj: {
    lotId: '/rcerp/agv/erp/findByLotNo',
    moduleNo: '/qms/immersionGold/potion/findLotNo'
  },
  modalClose() {
    lotType.value = 'searchInput';
    moduleNoSearchData.value = null;
    lotids.value = [];
  }
};
